package PrefrenceArea

import (
	"context"
	"encoding/json"

	"github.com/blog-zero/api/admin/internal/common/errorx"
	"github.com/blog-zero/api/admin/internal/svc"
	"github.com/blog-zero/api/admin/internal/types"
	"github.com/blog-zero/rpc/cms/cmsclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type PrefrenceAreaUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPrefrenceAreaUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PrefrenceAreaUpdateLogic {
	return &PrefrenceAreaUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PrefrenceAreaUpdateLogic) PrefrenceAreaUpdate(req *types.UpdatePrefrenceAreaReq) (*types.UpdatePrefrenceAreaResp, error) {
	_, err := l.svcCtx.PrefrenceAreaService.PrefrenceAreaUpdate(l.ctx, &cmsclient.PrefrenceAreaUpdateReq{
		Id:         req.Id,
		Name:       req.Name,
		SubTitle:   req.SubTitle,
		Pic:        req.Pic,
		Sort:       req.Sort,
		ShowStatus: req.ShowStatus,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("更新优选失败,参数:%s,异常:%s", reqStr, err.Error())
		return nil, errorx.NewDefaultError("更新优选失败")
	}

	return &types.UpdatePrefrenceAreaResp{
		Code:    "000000",
		Message: "更新优选成功",
	}, nil
}
